#################################################################################
# Replication file for the figures in
# 
# Mitrea, Elena Cristina, Monika M�hlb�ck, and Julia Rita Warmuth:
# �Extreme pessimists? Fear of socioeconomic decline and political attitudes�, 
# Political Behavior (forthcoming). 
#
#################################################################################
#
# Unless stated otherwise, the article draws on the youth survey data from the CUPESSE project
#
# The full CUPESSE dataset can be downloaded from Gesis (ZA7475)
#
#################################################################################
#################################################################################
#
# When using the data, please cite the data as follows:
# 
# Tosun, Jale; H�risch, Felix; Schuck, Bettina; Shore, Jennifer; Woywode, Michael; Strohmeyer, Robert; Kittel, Bernhard; Steiber, Nadia; Warmuth, Julia; M�hlb�ck, Monika; Lukes, Martin; Lorenc, Miroslav; Lorencov�, Hana; Pauknerov�, Daniela; Nov�, Ivan; Jensen, Carsten; Arndt, Christoph; Littvay, Levente; Vegetti, Federico; Sata, Robert; Balea, Elena C.; Caserta, Maurizio; Boindo, Alessio E.; Reito, Francesco; Monteleone, Simona; Arco, Jos� L.; Fernandez, Francisco; Hughes, Stephen; Carrillo, Francisco J.; Vancea, Mihaela; Jordana, Jacint; Freitag, Markus; Rapp, Carolin; Cemalcilar, Zeynep; S�mer, Nebi; Kamiloglu, Roza; Coskan, Canan; Maloney, William; Rainsford, Emily; Tsakloglou, Panos; Pierrakakis, Kyriakos; Christoforou, Asimina; Makantasi, Fay (2018): CUPESSE: Cultural Pathways to Economic Self-Sufficiency and Entrepreneurship. GESIS Data Archive, Cologne. ZA7475 Data file Version 1.0.0, doi:10.4232/1.13042
# 
#################################################################################
# 
# You have to execute stata do-file first
# 
#################################################################################

# install necessary packages

# install.packages("survey")
# install.packages("foreign")

# load necessary packages
library(survey)
library(foreign)

#################################################################################
# read data

d <- read.dta("data_extreme_pessimists_old.dta")

# weight data

ds_p <-svydesign(id=~1, weights=~PWEIGHT_1, data=d)


#################################################################################
# Figure 1
# Expected downward mobility among young adults

t1 <- prop.table(svytable(~YQ_expsocdec01+YQ_country, ds_p),2)*100
t1a <- t1[2,]

setEPS()
postscript(file="Figure1.eps", horizontal = FALSE, onefile = TRUE, paper = "special", width = 10, height = 6, pointsize = 12)
#win.metafile(file="Figure1.wmf", width = 10, height = 6, pointsize = 12)
par(mar=c(8,5,1,1))
barplot(t1a, main="", ylab="Percent", col="grey", ylim=c(0,35), las=2)
dev.off( ) 

#################################################################################
# Figure 2
# Density plots of left-right self-placement for those young adults who do expect downward mobility (black lines) and who do not (grey lines)


setEPS()
postscript(file="Figure2.eps", horizontal = FALSE, onefile = TRUE, paper = "special", width = 10, height = 12, pointsize = 12) 
#win.metafile(file="Figure2.wmf", width = 10, height = 12, pointsize = 12)
par(mfrow=c(4,3))

d1 <- na.omit(d[,c("YQ_leftright_0_10","YQ_expsocdec01","YQ_country","weights_equal_country")])

xlim <- range(d1$YQ_leftright_0_10, na.rm=T) # set range for x-axis
ylim <- range(0, 0.3) 

# all countries
dens1 <- density(d1$YQ_leftright_0_10[d1$YQ_expsocdec01==0], weight=d1$weights_equal_country[d1$YQ_expsocdec01==0]/sum(d1$weights_equal_country[d1$YQ_expsocdec01==0]), na.rm=T, adjust=3, from = xlim[1], to = xlim[2])
dens2 <- density(d1$YQ_leftright_0_10[d1$YQ_expsocdec01==1], weight=d1$weights_equal_country[d1$YQ_expsocdec01==1]/sum(d1$weights_equal_country[d1$YQ_expsocdec01==1]), na.rm=T, adjust=3, from = xlim[1], to = xlim[2])
plot(dens1$x, dens1$y, col = "grey65", lwd = 2, type = "l", xlim = xlim, ylim = ylim, ylab="Density", 
xlab="  Left                                                                    Right", main="All countries")
lines(dens2$x, dens2$y, col = 1, lwd = 2)

for(i in levels(d1$YQ_country))
{
dens1 <- density(d1$YQ_leftright_0_10[d1$YQ_expsocdec01==0 & d1$YQ_country==i], weight=d1$weights_equal_country[d1$YQ_expsocdec01==0 & d1$YQ_country==i]/sum(d1$weights_equal_country[d1$YQ_expsocdec01==0 & d1$YQ_country==i]), na.rm=T, adjust=3, from = xlim[1], to = xlim[2])
dens2 <- density(d1$YQ_leftright_0_10[d1$YQ_expsocdec01==1 & d1$YQ_country==i], weight=d1$weights_equal_country[d1$YQ_expsocdec01==1 & d1$YQ_country==i]/sum(d1$weights_equal_country[d1$YQ_expsocdec01==1 & d1$YQ_country==i]), na.rm=T, adjust=3, from = xlim[1], to = xlim[2])
plot(dens1$x, dens1$y, col = "grey65", lwd = 2, type = "l", xlim = xlim, ylim = ylim, ylab="Density", 
xlab="  Left                                                                    Right", main=i)
lines(dens2$x, dens2$y, col = 1, lwd = 2)
}
dev.off( ) 


#######################################################################################



